import Axios, { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse } from "axios";
import { message } from "antd";
/**
 * axios 拦截器
 */
export class Interceptors {
    public instance: AxiosInstance;
    constructor() {
        // 创建实例
        this.instance = Axios.create({ timeout: 1000 * 500 });

        // 初始化拦截器
        this.initInterceptors();
    }
    // 为了让http.ts中获取初始化好的axios实例
    public getInterceptors() {
        return this.instance;
    }
    //初始化拦截器
    public initInterceptors() {
        // 1. 设置post请求头
        this.instance.defaults.headers.post["Content-Type"] =
            "application/json;charset=utf-8";
        this.instance.defaults.headers["checkType"] = "ck-token-auth";
        // 2. 请求拦截器 -- 每次请求前，如果存在token则在请求头中携带token
        this.instance.interceptors.request.use(
            (config: InternalAxiosRequestConfig<any>) => {
                if (config.data) {
                    for (const key in config.data) {
                        if (config.data[key] === "") {
                            delete config.data[key];
                        }
                    }
                }
                // const token: string = 'a04c362f-157c-4a12-8e9d-4e41ec4e6de1';
                const token: string = sessionStorage.getItem("token") as string;
                const checkType: string = "ck-token-auth";
                if (token && config.headers && checkType) {
                    config.headers["satoken"] = token;
                    config.headers["checkType"] = checkType;
                }
                return config;
            },
            (error) => {
                console.log(error);
            }
        );
        // 3. 相应拦截器
        this.instance.interceptors.response.use(
            (res: AxiosResponse) => {
                if (res.status === 200) {
                    this.errorHandleCode(res);
                    return Promise.resolve(res.data);
                } else {
                    this.errorHandle(res);
                    return Promise.reject(res.data);
                }
            },
            (error) => {
                return Promise.reject(error); // 返回接口返回的错误信息
            }
        );
    }
    /**
     * http握手错误
     * @param res  响应回调,根据不同响应进行不同操作
     */
    private errorHandle(res: AxiosResponse) {

        // 状态码判断
        switch (res.data.code) {
            case 401:
                break;
            case 403:
                break;
            case 404:
                message.error("请求的资源不存在.");
                break;
            case 10002:
                window.location.href = "/";
                break;
            case 50004:
                window.location.href = "/";
                break;
            default:
                message.error("连接错误.");
        }
    }
    /**
     * 处理后端返回状态码
     * @param res  响应回调,根据不同响应进行不同操作
     */
    private errorHandleCode(res: AxiosResponse) {
        // 状态码判断
        switch (res.data.code) {
            case 10002:
                window.location.href = "/";
                break;
            case 50004:
                window.location.href = "/";
                break;
        }
    }
}
